/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.careersersystem.dao;

import java.util.ArrayList;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author Administrator
 */
public class UserDAO extends BaseHibernateDAO{
    private static final Log log= LogFactory.getLog(UserDAO.class);

    public ArrayList searchByUsername(String username){
        ArrayList result=new ArrayList();
        try{
            Configuration conf = new Configuration().configure();
            SessionFactory factory = conf.buildSessionFactory();
            Session session = factory.openSession();

            session.beginTransaction().begin();
            Query query = session.createQuery("SELECT u FROM Users u WHERE u.username = :username");
            query.setParameter("username",username);
            result.addAll(query.list());
        }catch(HibernateException ex){
            ex.printStackTrace();
        }

        return result;
    }
    public Iterator searchByUsernamePassword(String username, String userPassword){

        String hql = "SELECT u FROM User u WHERE u.username = :username and u.userPassword = :userPassword";

        Query query = getSession().createQuery(hql);
        if (username!=null&&userPassword!=null){
            query.setString("username", username);
            query.setString("userPassword", userPassword);
        }else{

        }
        Iterator result = query.iterate();

        return result;
    }
    public ArrayList searchByConfirmed(String status){
        short confirmed;
        if (status != null){
            ArrayList result = new ArrayList();
            if (status.equals("confirmed")){
                confirmed=1;
                String hql = "SELECT u WHERE u.confirmed = :confirmed";
                Query query = getSession().createQuery(hql);
                query.setParameter("confirmed", confirmed);
                result.addAll(query.list());

            }else{
                confirmed=0;
                String hql = "SELECT u WHERE u.confirmed = :confirmed";
                Query query = getSession().createQuery(hql);
                query.setParameter("confirmed", confirmed);
                result.addAll(query.list());

            }
            return result;
        }else{
            //throw Exception
            return null;
        }
    }
    public Iterator searchByUserType(short userType){
        String hql = "SELECT u WHERE u.userType = :userType";
        Query query = getSession().createQuery(hql);
        query.setParameter("userType", userType);
        Iterator result=query.iterate();
        return result;
    }
    public void addUser(){

    }
    public void updateUser(){

    }
    public void delectUser(){
        
    }

}

/**
 package com.assignment.hibernate;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

/**
 * Data access object (DAO) for domain model
 * @author MyEclipse Persistence Tools
 */
/**public class SearchDAO extends BaseHibernateDAO{
	private static final Log log= LogFactory.getLog(SearchDAO.class);
	private static final String AUTHOR_NAME = "author";
	private static final String TITLE_NAME ="title";


	public int getBookTotalCount(String title){
		String hql = "select count(*) from Books as b where 1=1";
		if(title!= null){
			hql += "and b.title like :title";
		}
		Query query= getSession().createQuery(hql);
		if (title!=null){
			query.setString("title", title);
		}
		List cc = query.list();
		Integer a = (Integer) cc.size();
		return a;
	}
	public int getTotalPage(int pageSize, String keyword){
		int totalCount = getBookTotalCount(keyword);
		int totalPageCount = ((totalCount + pageSize) -1 )/pageSize;
		return totalPageCount;
	}

	public List findPagedAll(int currentPage, int pageSize, Books instance){
		log.debug("search");
		try{
			if (currentPage == 0){
				currentPage = 1;
			}

			Criteria criteria =getSession().createCriteria(Books.class);
			Example example = Example.create(instance);
			if (instance.getTitle()!= null){
				example.excludeProperty("title");
			}
			criteria.add(example);
			if(instance.getTitle()!= null){
				criteria.add(Restrictions.ilike("title",instance.getTitle(),MatchMode.ANYWHERE));
			}
			criteria.setFirstResult((currentPage - 1) * pageSize);
			criteria.setMaxResults(pageSize);
			return criteria.list();
		}catch(RuntimeException re){
			log.error("find all failed", re);
			throw re;
		}
	}
}
 **/